﻿<template>
    <hxt-popup v-cloak v-model="show" closeable class="hidden" title="调寝/入住" round :close-on-click-overlay="false" style="height: 90%; max-height: 90%;">
        <component is="style" />
        <template v-if="!!user">
            <div class="flex-column h100 hidden" style="background-color: #F7F7F8;">
                <van-dropdown-menu active-color="#1989fa">
                    <van-dropdown-item v-model="dbiId" :options="buidingList"></van-dropdown-item>
                    <van-dropdown-item v-model="dfiId" :options="floorList"></van-dropdown-item>
                </van-dropdown-menu>
                <div class="flex-row margin-lr-15 margin-tb-4">
                    <van-icon class-prefix="x fa fa-circle" class="color-blue size-10" style="margin-right: 6px;"></van-icon>
                    <van-tag size="large" round type="primary">宿舍</van-tag>
                    <template v-if="room">
                        <div class="flex-row-fill margin-lr-10">
                            {{room.driName}} <span class="color-red margin-lr-10">{{room.driTypeStr}}</span>
                        </div>
                        <div>
                            <van-tag type="primary" class="jiaonan" style="font-size:10px;">共</van-tag>
                            <van-tag type="primary" plain style="font-size:10px;">{{room.driBunkCount}}</van-tag>
                            <van-tag type="success" class="margin-left-6 jiaonan" style="font-size:10px;">入住</van-tag>
                            <van-tag type="success" plain style="font-size:10px;">{{room.driCheckCount}}</van-tag>
                            <van-tag type="warning" class="margin-left-6 jiaonan" style="font-size:10px;">余</van-tag>
                            <van-tag type="warning" plain style="font-size:10px;">{{room.driBunkCount-room.driCheckCount}}</van-tag>
                        </div>
                    </template>
                </div>
                <div class="flex-column-fill h100">
                    <van-empty v-if="roomList.length==0" description="查无宿舍，请选择楼层后重试~"></van-empty>
                    <div v-else class="van-card bg-white" style="font-size:1.5rem;">
                        <span v-for="(item,index) in roomList" v-on:click="onChangeDRI(index)" :key="index" class="margin-4">
                            <van-tag size="large" round :plain="item.driId==driId?false:true" type="primary">{{item.driName}}</van-tag>
                        </span>
                    </div>
                </div>
                <div class="flex-row-stretch">
                    <van-divider></van-divider>
                    <div class="van-card bg-white padding" v-if="user">
                        <div class="flex-row">
                            <van-tag size="large" round color="#1cbbb4">{{user.uiType==0?'老师':'学生'}}</van-tag>
                            <van-icon v-if="user.uiSex==0" class-prefix="ico ico-female" class="color-red" title="女生"></van-icon>
                            <van-icon v-else class-prefix="ico ico-male" class="color-blue" title="男生"></van-icon>
                            <div class="flex-row-fill margin-lr-10">{{user.uiName}}</div>
                            <van-dropdown-menu active-color="#1989fa" direction="up" class="vdm-tag">
                                <van-dropdown-item v-model="user.sccId" :options="tagList"></van-dropdown-item>
                            </van-dropdown-menu>
                        </div>
                        <div class="flex-row margin-tb-6">
                            <div class="flex-row-fill color-999 margin-lr-10" v-if="!!user.giName||!!user.ciName">
                                {{user.giName}}|{{user.ciName}}
                            </div>
                            <div class="color-999">{{user.driName}}</div>
                        </div>
                    </div>
                    <div class="flex-row bg-white margin-bottom-10 padding-lr-4" style="justify-content: space-between;">
                        <van-button v-on:click="saveRoomUserAssign(0)" type="danger" v-if="!!user.driName" round block>退宿</van-button>
                        <van-button v-if="errMsg!=''" type="warning" round block plain>{{errMsg}}</van-button>
                        <van-button v-on:click="saveRoomUserAssign(1)" v-else type="info" round block>保存</van-button>
                    </div>
                </div>
            </div>
        </template>
    </hxt-popup>
</template>

<script>
    module = (function (hxt) {
        return {
            props: {
                value: {
                    type: Boolean,
                    'default': function () {
                        return false;
                    }
                },
                user: {
                    type: Object,
                    default: {}
                },
                item: {
                    type: Object,
                    default: {}
                },
            },
            name: 'hxt.dormtransfer',
            data: function () {
                return {
                    optor: hxt.store.optor,
                    type: 0,
                    dbiId: -1,
                    dfiId: -1,
                    driId: -1,
                    buidingList: [{
                        value: -1,
                        text: "选择楼栋"
                    }],
                    floorList: [{
                        value: -1,
                        text: "选择楼层"
                    }],
                    roomList: [{
                        value: -1,
                        text: "选择宿舍"
                    }],
                    tagList: [],
                    buiding: null,
                    floor: null,
                    room: null,
                    errMsg: '',

                };
            },
            watch: {
                show: {
                    handler: function (val, old) {
                        var _this = this;
                        if (val) {
                            _this.getBuidingList(true);
                            _this.getTagList();
                        } else {
                            _this.room = null;
                            _this.dbiId = -1;
                            _this.dfiId = -1;
                            _this.driId = -1;
                        }
                    },
                    immediate: true
                },
                user: {
                    handler: function (val, old) {
                        var _this = this;
                        if (!!val) {
                            _this.driId = val.driId;
                            _this.type = val.driId > 0 ? 1 : 0;
                        }
                    },
                    immediate: true
                },
                dbiId: {
                    handler: function (val, old) {
                        var _this = this;
                        var buiding = _this.buidingList.find(function (item) {
                            return item.dbiId == val;
                        });
                        if (!!buiding) _this.buiding = buiding;
                        _this.dfiId = -1;
                        _this.getFloorList();
                    },
                    immediate: true
                },
                dfiId: {
                    handler: function (val, old) {
                        var _this = this;
                        var floor = _this.floorList.find(function (item) {
                            return item.dfiId == val;
                        });
                        if (!!floor) _this.floor = floor;
                        _this.getRoomList();
                    },
                    immediate: true
                },
            },
            computed: {
                show: {
                    get: function () {
                        return this.value;
                    },
                    set: function (val) {
                        this.$emit('input', val);
                    }
                }
            },
            activated: function () {
                var _this = this;
            },
            mounted: function () {
                var _this = this;
            },
            methods: {
                close: function () {
                    var _this = this;
                    _this.show = false;
                },
                getBuidingList: function (flag) {
                    var _this = this;
                    _this.buidingList = [];
                    hxt.post({
                        url: hxt.config.link + 'dorm/dormBuidingList',
                        data: {
                            uiId: _this.optor.uiId,
                            uiType: _this.optor.uiType,
                            bsiId: _this.user.bsiId
                        },
                        ok: function (res) {
                            if (res.code != 0) {
                                hxt.alert(res.errmsg);
                                return;
                            }
                            var buidingList = [];
                            res.data.forEach(function (item, idx) {
                                item.value = item.dbiId;
                                item.text = item.dbiName;
                                buidingList.push(item);
                            });
                            _this.buidingList = buidingList;
                            var arr = !!_this.user.driName ? _this.user.driName.split("/") : [];
                            if (arr.length > 0) {
                                var dbi = _this.buidingList.find(function (item) {
                                    return item.text == arr[0];
                                });
                                _this.buiding = dbi;
                                _this.dbiId = !!dbi.value ? dbi.value : 0;
                            } else {
                                if (_this.buidingList.length > 0) {
                                    _this.dbiId = _this.buidingList[0].value;
                                    _this.buiding = _this.buidingList[0];
                                };
                            };
                        },
                        fail: function (res) {
                            hxt.alert(res.errMsg);
                        }
                    });
                },
                getFloorList: function (flag) {
                    var _this = this;
                    _this.floorList = [];
                    if (_this.dbiId < 1) return;
                    hxt.post({
                        url: hxt.config.link + 'dorm/dormFloorList',
                        data: {
                            uiId: _this.optor.uiId,
                            dbiId: _this.dbiId,
                            uiType: _this.optor.uiType,
                            bsiId: _this.user.bsiId
                        },
                        ok: function (res) {
                            if (res.code != 0) {
                                hxt.alert(res.errmsg);
                                return;
                            }
                            var floorList = [];
                            res.data.forEach(function (item, idx) {
                                item.value = item.dfiId;
                                item.text = item.dfiName;
                                floorList.push(item);
                            });
                            _this.floorList = floorList;
                            var arr = !!_this.user.driName ? _this.user.driName.split("/") : [];
                            if (arr.length > 1) {
                                var dfi = _this.floorList.find(function (item) {
                                    return item.text == arr[1];
                                });
                                _this.floor = dfi;
                                _this.dfiId = !!dfi.value ? dfi.value : 0;
                            } else {
                                if (_this.floorList.length > 0) {
                                    _this.dfiId = _this.floorList[0].value;
                                    _this.floor = _this.floorList[0];
                                };
                            }
                        }
                    });
                },
                getRoomList: function () {
                    var _this = this;
                    _this.roomList = [];
                    if (_this.dfiId < 0) return;
                    hxt.post({
                        url: hxt.config.link + 'dorm/dormRoomList',
                        data: {
                            uiId: _this.optor.uiId,
                            dfiId: _this.dfiId,
                            uiType: _this.optor.uiType,
                            bsiId: _this.user.bsiId
                        },
                        ok: function (res) {
                            if (res.code != 0) {
                                hxt.alert(res.errmsg);
                                return;
                            }

                            _this.roomList = res.data;

                            if (_this.user.driId) {
                                var room = _this.roomList.find(function (item) {
                                    return item.driId == _this.user.driId;
                                });
                                if (!!room) _this.room = room;
                            }
                        }
                    });
                },
                getTagList: function () {
                    var _this = this;
                    _this.tagList = [];
                    hxt.post({
                        url: hxt.config.link + 'pc/selectList',
                        data: {
                            uiId: _this.optor.uiId,
                            scc: 1,
                            uiType: _this.optor.uiType,
                            bsiId: _this.user.bsiId
                        },
                        ok: function (res) {
                            if (res.code != 0) {
                                hxt.alert(res.errmsg);
                                return;
                            }

                            var tagList = [];
                            res.data.sccList.forEach(function (item, idx) {
                                item.value = item.sccId;
                                item.text = item.sccName;
                                if (item.sccStatus != 2) tagList.push(item);
                            });
                            _this.tagList = tagList;
                        }
                    });
                },
                onChangeDRI: function (index) {
                    var _this = this, room = _this.roomList[index];
                    if (_this.driId == room.driId) return;
                    _this.driId = room.driId
                    _this.room = room;

                    var typeStr = (_this.user.flag == 1 ? "调寝" : "入住");
                    if (_this.buiding.dbiStatus > 1) {
                        _this.errMsg = '楼栋已禁用，不可' + typeStr;
                        return;
                    }

                    if (_this.floor.dfiStatus > 1) {
                        _this.errMsg = errMsg = '楼层已禁用，不可' + typeStr;
                        return;
                    }

                    if (_this.room.driStatus > 1) {
                        _this.errMsg = errMsg = '宿舍已禁用，不可' + typeStr;
                        return;
                    }

                    if (_this.user.uiType == 0 && _this.room.driType != 2) {
                        _this.errMsg = errMsg = '教师与宿舍房间类型不匹配，不可' + typeStr;
                        return;
                    }
                    if (_this.user.uiType != 0 && _this.room.driType != 3 && _this.room.driType != _this.user.uiSex) {
                        _this.errMsg = errMsg = '学生与宿舍房间类型不匹配，不可' + typeStr;
                        return;
                    }
                    if (_this.room.driCheckCount + 1 > _this.room.driBunkCount) {
                        _this.errMsg = '已满员，不可' + typeStr;
                        return;
                    }
                    _this.errMsg = '';
                },
                saveRoomUserAssign: function (type) {
                    var _this = this, user = _this.user;
                    if (type == 0) {
                        hxt.confirm('确认将【' + user.uiName + '】从【' + user.driName + '】退宿吗？',
                            function () {
                                hxt.post({
                                    url: hxt.config.link + 'dorm/roomUserAssign',
                                    data: {
                                        uiId: _this.optor.uiId,
                                        uiType: _this.optor.uiType,
                                        flag: 2,
                                        uiIdq: user.uiId,
                                        sccId: user.sccId,
                                        bsiId: user.bsiId
                                    },
                                    ok: function (res) {
                                        if (res.code != 0) return hxt.alert("退宿失败:" + res.errmsg);
                                        vant.Notify({ type: 'success', message: '退宿成功' });
                                        var tag = _this.tagList.find(function (item) {
                                            return item.sccId == user.sccId;
                                        });

                                        var item = { sccName: tag.sccName, dbiName: '', dfiName: '', driName: '', BFName: '', sccId: user.sccId, driId: 0 };
                                        _this.$emit('update:item', item);
                                        _this.close();
                                    }
                                });
                            });
                    };
                    if (type == 1) {
                        var typeStr = (_this.type == 1 ? "调寝" : "入住");
                        hxt.post({
                            url: hxt.config.link + 'dorm/roomUserAssign',
                            data: {
                                uiId: _this.optor.uiId,
                                flag: user.flag,
                                uiIdq: user.uiId,
                                driId: _this.driId,
                                sccId: user.sccId,
                                uiType: _this.optor.uiType,
                                bsiId: user.bsiId
                            },
                            ok: function (res) {
                                if (res.code != 0) return hxt.alert(typeStr + "失败:" + res.errmsg);
                                vant.Notify({ type: 'success', message: typeStr + "成功" });
                                var user = _this.user;
                                var tag = _this.tagList.find(function (item) {
                                    return item.sccId == user.sccId;
                                });

                                var item = {
                                    sccName: tag.sccName,
                                    dbiName: _this.buiding.dbiName,
                                    dfiName: _this.floor.dfiName,
                                    driName: _this.room && _this.room.driName ? _this.room.driName : user.driName,
                                    BFName: _this.buiding.dbiName + '|' + _this.floor.dfiName,
                                    sccId: user.sccId,
                                    driId: _this.driId
                                };
                                _this.$emit('update:item', item);
                                _this.close();
                            }
                        });
                    };
                },

            },
            components: {

            }
        };
    })(window.$jscript.fn);
</script>
<style>
    .vdm-tag .van-dropdown-menu__bar {
        box-shadow: none !important;
        height: auto !important;
    }

    .vdm-tag .van-dropdown-menu__title {
        color: #1989fa !important;
    }
</style>